import store from "../store/index.js"
let BASE_URL = 'https://api.microapp.eachiing.com/api/' //请求数据地址
// let BASE_URL = 'http://wm.cc/api/' //请求数据地址
export const Request = (options) => {
	return new Promise((resolve, reject) => {
		let header = {
			accept: "application/json"
		}
		if (options?.auth) {
			header.Authorization = uni.getStorageSync('token')
		}
		uni.request({
			url: BASE_URL + options.url,
			method: options.method || 'GET',
			data: options.data || {},
			header,
			success: (res) => {
				resolve(res.data)
				if (res.header.token) {
					let userinfo = uni.getStorageSync("userinfo");
					let token = res.header.token;
					uni.setStorageSync("token", token)
					if (userinfo) {
						userinfo.token = res.header.token
						store._mutations.login[0](userinfo);
					}
				}
			},
			fail: (err) => {
				uni.showToast({
					title: '网络错误',
					icon: "none"
				})
				reject(err)
			}
		})
	})
}
